package demo1;

import java.util.List;

public class LinkNodes implements IList {
        static class LinkList{
            public int val;
            public LinkList prev;
            public LinkList next;
            public LinkList(int val) {
                this.val = val;
            }}
        public LinkList head;
        public LinkList last;
        public void createLink() {
            LinkList pot1 = new LinkList(1);
            LinkList pot2 = new LinkList(3);
            LinkList pot3 = new LinkList(4);
            LinkList pot4 = new LinkList(5);
            LinkList pot5 = new LinkList(6);
            pot1.next = pot2;
            pot2.next = pot3;
            pot3.next = pot4;
            pot4.next = pot5;
            this.head=pot1;
        }

        @Override
        public void display() {
            LinkList cur1 = head;
            while (cur1 != null) {
                System.out.print(cur1.val+" ");
                cur1 = cur1.next;
            }
        }

        @Override
        public int size() {
            LinkList cur1 = head;
            int len = 0;
            while (cur1 != null) {
                len++;
                cur1 = cur1.next;
            }
            return len;
        }

        @Override
        public boolean contains(int date) {
            LinkList cur1 = head;
            while (cur1 != null) {
                if (cur1.val == date) {
                    return true;
                }
                cur1 = cur1.next;
            }
            return false;
        }



    //头插法
        public void addFirst(int date) {
            LinkList cur1 = head;

            LinkList pot = new LinkList(date);
            if(head==null){
                head=pot;
            }else {
                pot.next = head;
                head.prev=pot;
                head = pot;
            }
        }

        //尾插
        public void addLast(int date) {
            LinkList pot = new LinkList(date);
            if (head == null) {
                head = pot;
            }else {
            LinkList cur1=head;
            while (cur1.next != null) {
                cur1 = cur1.next;
            }
            cur1.next = pot;
        }}
        //任意位置插入

        @Override
        public void addIndex(int index, int date) throws IndexError {
            try {
                if (index == 0) {
                    addFirst(date);
                    return;
                }
                if (index == size()) {
                    addLast(date);
                }
                if (index < 0 || index > size()) {
                    throw new IndexError();
                }
                LinkList cur1 = find(index);
                LinkList pot = new LinkList(date);
                pot.next = cur1.next;
                cur1.next = pot;
            } catch (IndexError e) {
                System.out.println("位置越界");
            }
        }
        private LinkList findIndex(int index){
            LinkList cur=head;
            while(index-1!=0){
                cur=cur.next;
                index--;
            }
            return cur;
        }
        //删除

        @Override
        public void remove(int date) {
            if (head == null) {
                return;
            }
            LinkList cur1 = find(date);
            if (cur1 == null) {
                System.out.println("没有这个数！！！");
                return;
            }
            LinkList del = cur1.next;
            cur1.next = del.next;
        }

        public LinkList find(int date) {
            LinkList cur = head;
            while (cur.next != null) {
                if (cur.next.val == date) {
                    return cur;
                }
                cur = cur.next;
            }
            return null;
        }

        //删除
        public void removeList(int date) {
            if (head == null) {
                return;
            }
            LinkList prev = head;
            LinkList cur = head.next;
            while (cur != null) {
                if (cur.val == date) {  //不含头点
                    prev.next = cur.next;
                    cur = cur.next;
                } else {
                    prev = cur;
                    cur = cur.next;
                }
            }
            if (head.val == date) {//如果头点是单独删除
                head = head.next;
            }
        }
        //清除

        @Override
        public void clear() {
            LinkList cur = head;
            while (cur != null) {
                LinkList cur2 = cur.next;
                cur.next = null;
                cur = cur2;
            }
            head = null;//处理漏掉的头结点
        }
    }



